<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="序号" prop="serialNum">
      <el-input v-model="dataForm.serialNum" placeholder="序号"></el-input>
    </el-form-item>
    <el-form-item label="类型" prop="type">
      <el-input v-model="dataForm.type" placeholder="类型"></el-input>
    </el-form-item>
    <el-form-item label="产品名称" prop="productName">
      <el-input v-model="dataForm.productName" placeholder="产品名称"></el-input>
    </el-form-item>
    <el-form-item label="基本功能" prop="basicFunction">
      <el-input v-model="dataForm.basicFunction" placeholder="基本功能"></el-input>
    </el-form-item>
    <el-form-item label="适配平台" prop="adaptationPlatform">
      <el-input v-model="dataForm.adaptationPlatform" placeholder="适配平台"></el-input>
    </el-form-item>
    <el-form-item label="性能参数" prop="performanceParameter">
      <el-input v-model="dataForm.performanceParameter" placeholder="性能参数"></el-input>
    </el-form-item>
    <el-form-item label="厂家联系方式" prop="contactInformation">
      <el-input v-model="dataForm.contactInformation" placeholder="厂家联系方式"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          serialNum: '',
          type: '',
          productName: '',
          basicFunction: '',
          adaptationPlatform: '',
          performanceParameter: '',
          contactInformation: ''
        },
        dataRule: {
          serialNum: [
            { required: true, message: '序号不能为空', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '类型不能为空', trigger: 'blur' }
          ],
          productName: [
            { required: true, message: '产品名称不能为空', trigger: 'blur' }
          ],
          basicFunction: [
            { required: true, message: '基本功能不能为空', trigger: 'blur' }
          ],
          adaptationPlatform: [
            { required: true, message: '适配平台不能为空', trigger: 'blur' }
          ],
          performanceParameter: [
            { required: true, message: '性能参数不能为空', trigger: 'blur' }
          ],
          contactInformation: [
            { required: true, message: '厂家联系方式不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/generator/aubaseproductsdirectories/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.serialNum = data.aubaseproductsdirectories.serialNum
                this.dataForm.type = data.aubaseproductsdirectories.type
                this.dataForm.productName = data.aubaseproductsdirectories.productName
                this.dataForm.basicFunction = data.aubaseproductsdirectories.basicFunction
                this.dataForm.adaptationPlatform = data.aubaseproductsdirectories.adaptationPlatform
                this.dataForm.performanceParameter = data.aubaseproductsdirectories.performanceParameter
                this.dataForm.contactInformation = data.aubaseproductsdirectories.contactInformation
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/aubaseproductsdirectories/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'serialNum': this.dataForm.serialNum,
                'type': this.dataForm.type,
                'productName': this.dataForm.productName,
                'basicFunction': this.dataForm.basicFunction,
                'adaptationPlatform': this.dataForm.adaptationPlatform,
                'performanceParameter': this.dataForm.performanceParameter,
                'contactInformation': this.dataForm.contactInformation
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
